﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CDiese.Actions;

namespace TuongLai_Schools
{
    public enum DataState
    {
        Insert,
        Edit,
        View
    };
    public partial class ucDataButton : UserControl
    {
        #region Declare variables
        private DataState nState = DataState.View;
        private bool bIsContinue = true;
        public delegate void DataHandler();
        public event DataHandler InsertHandler;
        public event DataHandler EditHandler;
        public event DataHandler SaveHandler;
        public event DataHandler DeleteHandler;
        public event DataHandler CancelHandler;
        public event DataHandler CloseHandler;

        #endregion

        public ucDataButton()
        {
            InitializeComponent();
        }
        public DataState DataMode
        {
            get
            {
                return nState;
            }
            set
            {
                nState = value;
            }
        }

        public bool IsContitnue
        {
            get
            {
                return bIsContinue;
            }
            set
            {
                bIsContinue = value;
            }
        }

        #region Set focus buttons
        public void SetInsertFocus()
        {
            cmdAdd.Enabled = true;
            cmdAdd.Focus();
        }

        public void SetSaveFocus()
        {
            cmdSave.Focus();
        }

        public void SetCancelFocus()
        {
            cmdCancel.Focus();
        }

        public void SetEditFocus()
        {
            cmdEdit.Focus();
        }

        public void SetDeleteFocus()
        {
            cmdDelete.Focus();
        }

        #endregion

        #region Attribute Button
        private bool addNewVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool AddNewVisible
        {
            set
            {
                addNewVisible = value;
                cmdAdd.Visible = addNewVisible;
            }
            get
            {
                return addNewVisible;
            }
        }

        private bool editVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool EditVisible
        {
            set
            {
                editVisible = value;
                cmdEdit.Visible = editVisible;
            }
            get
            {
                return editVisible;
            }
        }

        private bool deleteVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool DeleteVisible
        {
            set
            {
                deleteVisible = value;
                cmdDelete.Visible = deleteVisible;
            }
            get
            {
                return deleteVisible;
            }
        }

        private bool saveVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool SaveVisible
        {
            set
            {
                saveVisible = value;
                cmdSave.Visible = saveVisible;
            }
            get
            {
                return saveVisible;
            }
        }

        private bool cancelVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool CancelVisible
        {
            set
            {
                cancelVisible = value;
                cmdCancel.Visible = cancelVisible;
            }
            get
            {
                return cancelVisible;
            }
        }

        private bool CloseVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool closeVisible
        {
            set
            {
                CloseVisible = value;
                cmdClose.Visible = closeVisible;
            }
            get
            {
                return CloseVisible;
            }
        }

        private bool excelVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool ExcelVisible
        {
            set
            {
                excelVisible = value;
                cmdExcel.Visible = ExcelVisible;
            }
            get
            {
                return excelVisible;
            }
        }

        private bool reportVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool ReportVisible
        {
            set
            {
                reportVisible = value;
                cmdReport.Visible = ReportVisible;
            }
            get
            {
                return reportVisible;
            }
        }

        private bool printVisible = true;
        [Category("ButonVisible")]
        [Browsable(true)]
        public bool PrintVisible
        {
            set
            {
                printVisible = value;
                cmdPrint.Visible = PrintVisible;
            }
            get
            {
                return printVisible;
            }
        }
        #endregion

        #region Update State Buttons

        private void acAdd_Update(object sender, EventArgs e)
        {
            (sender as CDiese.Actions.Action).Enabled = (nState != DataState.Insert);
        }

        private void acEdit_Update(object sender, EventArgs e)
        {
            (sender as CDiese.Actions.Action).Enabled = (nState != DataState.Insert);
        }

        private void acDelete_Update(object sender, EventArgs e)
        {
            (sender as CDiese.Actions.Action).Enabled = (nState != DataState.Insert);
        }

        private void acSave_Update(object sender, EventArgs e)
        {
            (sender as CDiese.Actions.Action).Enabled = (nState != DataState.View);
        }

        private void acCancel_Update(object sender, EventArgs e)
        {
            (sender as CDiese.Actions.Action).Enabled = (nState != DataState.View);
        }
        #endregion

        #region imolement Button

        private void acClose_Execute(object sender, EventArgs e)
        {
            if (CloseHandler != null)
                CloseHandler();
            else
            {
                this.ParentForm.Close();
            }
        }

        private void acAdd_Execute(object sender, EventArgs e)
        {
            nState = DataState.Insert;
            if (InsertHandler != null)
                InsertHandler();
        }

        private void acEdit_Execute(object sender, EventArgs e)
        {
            nState = DataState.Edit;
            if (EditHandler != null)
                EditHandler();
        }

        private void acDelete_Execute(object sender, EventArgs e)
        {
            if (DeleteHandler != null)
                DeleteHandler();
        }

        private void acSave_Execute(object sender, EventArgs e)
        {
            if (SaveHandler != null)
                SaveHandler();
        }

        private void acCancel_Execute(object sender, EventArgs e)
        {
            if (CancelHandler != null)
                CancelHandler();

            nState = DataState.View;
        }
        #endregion
    }
}
